package com.itwxz.system.controller.mobile;

import com.itwxz.common.constant.CacheConstants;
import com.itwxz.common.core.domain.R;
import com.itwxz.common.core.redis.RedisCache;
import com.itwxz.common.utils.SecurityUtils;
import com.itwxz.common.utils.bean.BeanUtils;
import com.itwxz.system.domain.po.WxzConfig;
import com.itwxz.system.domain.vo.WxzConfigVo;
import com.itwxz.system.service.IWxzConfigService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Tag(name = "维修站手机端配置Controller")
@Slf4j
@RestController
@RequestMapping("/mobile/config")
@RequiredArgsConstructor
public class WxzConfigMobileController {
    private final IWxzConfigService configService;

	private final RedisCache  redisCache;

    @GetMapping("/repair")
    @Cacheable(value = "config", key = "'repair'")
    public R<Map<String, String>> getRepairConfig() {
        Map<String, String> result = configService.lambdaQuery()
                .likeRight(WxzConfig::getConfigKey, "repair")
                .list()
                .stream()
                .map(config -> BeanUtils.copyBean(config, WxzConfigVo.class))
                .collect(Collectors.toMap(WxzConfigVo::getConfigKey, WxzConfigVo::getConfigValue));

        return R.ok(result);
    }

    @GetMapping("/staff")
    public R<Map<String, Object>> getStaffConfig() {
        return R.ok(configService.getStaffConfig());
    }

	@GetMapping("/tool-location")
	public R<String> getToolLocation() {
		// 从 Redis 获取
		String toolLocation = redisCache
				.getCacheObject(CacheConstants.TOOL_LOCATION);
		// 位置丢失
		if (toolLocation == null) {
			toolLocation = "位置丢失...";
		}
		return R.ok(toolLocation);
	}

	@GetMapping("/tool-location/update")
	public R<Void> updateToolLocation(@RequestParam String toolLocation) {
		if (toolLocation == null || toolLocation.trim().isEmpty()) {
			return R.fail("工具箱位置不能为空");
		}
		// 更新 Redis
		redisCache.setCacheObject(CacheConstants.TOOL_LOCATION, toolLocation);
		return R.ok();
	}


}
